package com.daxia.ogmall.util;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.daxia.ogmall.common.Logs;

/**
 * 工具类
 * */
public class JsoupUtil {
	
	// get请求
	public static String getOldDWByGet(String objUrl) {
		Document doc = null;
		String dataStr = null;
		try {
			doc = Jsoup.connect(objUrl).timeout(20000).get();
			dataStr = doc.text();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dataStr;
	}
	
	// post请求
	public static String getOldDWByPost(String objUrl) {
		Document doc = null;
		String dataStr = null;
		try {
			doc = Jsoup.connect(objUrl).timeout(10000).post();
			dataStr = doc.text();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return dataStr;
	}
	

	public static String parseDocument(String document) {
		String ret = "";
		
		Document doc = Jsoup.parse(document);
		Elements elements = doc.select("img");
		for (Element element : elements) {
			element.attr("style", "width:100%");
			String src = element.attr("src");
			element.attr("src", src + "?imageMogr2/auto-orient/quality/30&imageView2/2/w/400");
		}
		ret = doc.body().html();
		return ret;
	}
	
	
	public static void main(String[] args) {
		String str = parseDocument("<img></img>");
		Logs.COMMON.debug(str);
	}
}
